<%@ page language="java" import="java.util.*,com.css.vo.Car" pageEncoding="UTF-8"%>
<%@ page import="com.css.dao.*,com.css.dao.impl.*"%>
<%@ page import="org.springframework.context.*,org.springframework.context.support.*"%>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>汽车信息管理</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <header>
    <h1>汽车信息管理</h1>
    <nav>
      <ul>
        <li><a href="index.jsp">首页</a></li>
        <li><a href="getList.action">系统管理</a></li>
        <li><a href="customer-management.jsp">客户信息管理</a></li>
        <li><a href="car-management.jsp">汽车信息管理</a></li>
        <li><a href="employee-management.jsp">员工信息管理</a></li>
        <li><a href="sales-management.jsp">销售信息管理</a></li>
      </ul>
    </nav>
  </header>
  <!-- 在表格上方添加搜索框 -->

  <div class="content">
    <h2>汽车信息列表</h2>
    <!-- 搜索框 -->
        <div class="search-box">
            <form action="searchCar.action" method="post">
                <input type="text" name="keyword" placeholder="输入关键字搜索">
                <button type="submit">搜索</button>
            </form>
        </div>
    <!-- 新增按钮 -->
    <button onclick="location.href='add-car.jsp'">新增汽车</button>
    <table border="1">
      <thead>
        <tr>
          <th>汽车ID</th>
          <th>车型</th>
          <th>版本</th>
          <th>年款</th>
          <th>颜色</th>
          <th>价格</th>
          <th>库存数量</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
         <% 
                ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
                ICarDAO carDAO = (ICarDAO)ctx.getBean("carDAO");
                String keyword = request.getParameter("keyword");
                List<Car> carList;
                
                if(keyword != null && !keyword.trim().isEmpty()) {
                    carList = carDAO.searchCar(keyword);
                } else {
                    carList = carDAO.findAllCar();
                }
                
                if(carList != null && !carList.isEmpty()) {
                    for(Car car : carList) {
                %>
        <tr>
          <td><%= car.getCarId() %></td>
          <td><%= car.getCarModel() %></td>
          <td><%= car.getVersion() %></td>
          <td><%= car.getYear() %></td>
          <td><%= car.getColor() %></td>
          <td><%= car.getPrice() %></td>
          <td><%= car.getStock() %></td>
          <td><%= car.getStatus() %></td>
          <td>
          	  <form action="updateCarView.action" method="post" style="display:inline;">
                            <input type="hidden" name="carId" value="<%= car.getCarId() %>">
                            <button type="submit">修改</button>
                        </form>
                        <form action="deleteCar.action" method="post" style="display:inline;" 
                              onsubmit="return confirm('确定删除该汽车信息吗?');">
                            <input type="hidden" name="carId" value="<%= car.getCarId() %>">
                            <button type="submit">删除</button>
                        </form>
          </td>
        </tr>
                <%
                    }
                } else {
                %>
                <tr>
                    <td colspan="9" style="text-align: center;">没有找到匹配的记录</td>
                </tr>
                <% } %>
      </tbody>
    </table>
  </div>
</body>
</html>
